package com.loongtech.bi.action.report;

import com.loongtech.bi.action.UIHelper;
import com.loongtech.bi.entity.pay.EntityNewUserLTV;
import com.loongtech.bi.manager.pay.NewUserLTVManager;
import com.loongtech.bi.manager.report.GameReportManager;
import com.loongtech.bi.manager.system.SysLogManager;
import com.loongtech.bi.support.Helper;
import com.loongtech.bi.support.Session;
import com.loongtech.core.util.Time;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import javax.annotation.PostConstruct;
import java.util.*;

/**
 * 运营报表-渠道累计付费Action
 *
 * @author Marin
 * @date 2015年09月17日
 */
@Controller
@Scope("view")
public class ChannelPayReportAction {
    private String dateRadio = "7";  // 切换的天数：7=近7天。15=近15天。30=近30天，0=自选时间
    private Date dateStart = new Date(Time.getBeginningOfDay(Time.currentTime() - 7 * Time.DAY));  // 自选时间：开始时间
    private Date dateEnd = new Date(Time.getBeginningOfDay(Time.currentTime() - Time.DAY));  // 自选时间：结束时间

    private List<GameReportManager.ChannelPayData> channelPayDatas;

    @Autowired
    NewUserLTVManager newUserLTVManager;
    @Autowired
    GameReportManager gameReportManager;

    @Autowired
    private SysLogManager sysLogManager;
    @Autowired
    private Session session;

    @PostConstruct
    public void init() {
        search();
    }

    public void search() {
        sysLogManager.addLog(session.getUserName(), "查询运营报表-渠道累计付费");

        channelPayDatas = new ArrayList<>();

        Date startDay = new Date(Time.getBeginningOfDay(("0".equals(dateRadio) ? dateStart : new Date(Time.getBeginningOfDay(Time.currentTime() - (Integer.parseInt(dateRadio) + 1) * Time.DAY))).getTime()));
        Date endDay = new Date(Time.getBeginningOfDay(("0".equals(dateRadio) ? dateEnd : new Date(Time.getBeginningOfDay(Time.currentTime() - Time.DAY))).getTime()));

        List<EntityNewUserLTV> list13 = newUserLTVManager.getChannelNewUserLTVByDate(dateStart, dateEnd);
        if (!Helper.isEmptyValue(list13)) {

            Map<String, GameReportManager.ChannelPayData> tmpMap = new LinkedHashMap<>();

            for (EntityNewUserLTV o : list13) {
                int d = Time.getInterval(Time.formatDate(o.getStartday(), "yyyy-MM-dd"), Time.formatDate(o.getEndday(), "yyyy-MM-dd")) + 1;
                if (d <= 90) {
                    String key = o.getKey();
                    GameReportManager.ChannelPayData tmp = tmpMap.get(key);
                    if (tmp == null) {
                        tmp = gameReportManager.new ChannelPayData();
                        tmp.setDateStr(o.getStartday());
                        tmp.setPlatformId(o.getPlatformid());
                        tmp.setChannelId(o.getChannelid());
                        tmpMap.put(key, tmp);
                    }

                    tmp.setIncome(d, UIHelper.getYuan(o.getCashadd()));
                }
            }

            channelPayDatas.addAll(new ArrayList<>(tmpMap.values()));
            Collections.sort(channelPayDatas,gameReportManager.new ChannelPayData().comparatorDate2);
        }
    }

    /**
     * 切换天数
     */
    public void radioChange() {
        int stayDay = "0".equals(dateRadio) ? 2 : Integer.parseInt(dateRadio);
        dateStart = new Date(Time.getBeginningOfDay(Time.currentTime() - stayDay * Time.DAY));

        search();
    }

    /**
     * 划动天数
     */
    public void spinnerChange() {
        search();
    }

    public Date getDateStart() {
        return dateStart;
    }

    public void setDateStart(Date dateStart) {
        this.dateStart = dateStart;
    }

    public Date getDateEnd() {
        return dateEnd;
    }

    public void setDateEnd(Date dateEnd) {
        this.dateEnd = dateEnd;
    }

    public String getDateRadio() {
        return dateRadio;
    }

    public void setDateRadio(String dateRadio) {
        this.dateRadio = dateRadio;
    }

    public List<GameReportManager.ChannelPayData> getChannelPayDatas() {
        return channelPayDatas;
    }

    public void setChannelPayDatas(List<GameReportManager.ChannelPayData> channelPayDatas) {
        this.channelPayDatas = channelPayDatas;
    }
}
